/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "behaviortree.h"

using namespace Try;

BehaviorTree::BehaviorTree(BehaviorTree* parent, const BehaviorNodeList& nodes)
    :   BehaviorNode(parent)
{
    for (int i = 0; i < nodes.size(); i++)
        this->addNode(nodes[i]);
}

BehaviorTree::~BehaviorTree()
{
    this->removeAllNodes();
}

int BehaviorTree::nodeCount() const
{
    return m_children.size();
}

BehaviorNode* BehaviorTree::node(int id) const
{
    if (id >= 0 && id < m_children.size())
        return m_children.at(id);

    return 0;
}

int BehaviorTree::addNode(BehaviorNode* node)
{
    if (node)
    {
        int count = m_children.size();

        for (int i = 0; i < count; i++)
            if (m_children[i] == node)
                return i;

        BehaviorTree* prev_parent = static_cast<BehaviorTree*>(node->parent());
        if (prev_parent)
            prev_parent->removeNode(node);

        m_children.push_back(node);
        node->setParent(this);

        return count;
    }

    return -1;
}

void BehaviorTree::removeNode(int id)
{
    if (id >= 0 && id < m_children.size())
    {
        BehaviorNode* node = m_children[id];
        m_children.erase(m_children.begin()+id);
        node->setParent(0);
    }
}

void BehaviorTree::removeNode(BehaviorNode* node)
{
    for (int i = 0; i < m_children.size(); i++)
    {
        if (m_children[i] == node)
        {
            this->removeNode(i);
            break;
        }
    }
}

void BehaviorTree::removeAllNodes()
{
    for (int i = m_children.size(); i > 0; i--)
        this->removeNode(i-1);
}

void BehaviorTree::deleteNode(int id)
{
    if (id >= 0 && id < m_children.size())
    {
        BehaviorNode* node = m_children[id];
        m_children.erase(m_children.begin()+id);
        delete node;
    }
}

void BehaviorTree::deleteNode(BehaviorNode* node)
{
    for (int i = 0; i < m_children.size(); i++)
    {
        if (m_children[i] == node)
        {
            this->deleteNode(i);
            break;
        }
    }
}

void BehaviorTree::deleteAllNodes()
{
    for (int i = m_children.size(); i > 0; i--)
        this->deleteNode(i-1);
}

void BehaviorTree::reset()
{
    for (int i = 0; i < m_children.size(); i++)
        m_children[i]->reset();

    BehaviorNode::reset();
}

void BehaviorTree::forwardMessageToNode(int id, const Message& msg, Object* sender, Property* property)
{
    if (id >= 0 && id < m_children.size())
    {
        BehaviorNode* node = m_children[id];
        if (node)
            node->onMessage(msg, sender, property);
    }
}
